<script setup>
import {
  addDictionaryData,
  deleteDictionaryData,
  updateDictionaryData,
  queryDictionaryData
} from '@/api/system/dictionary.js'
import { ElMessage, ElMessageBox } from 'element-plus'
import { Search, Refresh, Plus } from '@element-plus/icons-vue'
import { usePagination } from '@/hooks/usePagination'
import AddDictionaryValue from './components/AddDictionaryValue.vue'

const route = useRoute()

const loading = ref(false)
const { paginationData, handleCurrentChange, handleSizeChange } = usePagination()
const addDictionaryValueRef = ref(null)
const pageData = reactive({
  isShowAddDialog: false,
  editingId: ''
})

const createDic = (record) => {
  const { query } = route
  addDictionaryData({
    ...record,
    sysDictTypeId: query.dictTypeId
  }).then(() => {
    ElMessage.success('新增成功')
    pageData.isShowAddDialog = false
    getTableData()
  }).finally(()=>{
    addDictionaryValueRef.value.submitBtnLoading = false
  })
}

const updateDic = (record) => {
  updateDictionaryData(record).then(() => {
    ElMessage.success('修改成功')
    pageData.isShowAddDialog = false
    getTableData()
  }).finally(()=>{
    addDictionaryValueRef.value.submitBtnLoading = false
  })
}

const handleDelete = (row) => {
  ElMessageBox.confirm(`正在删除字典值：${row.dictLabel}，确认删除？`, 'Warning', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    deleteDictionaryData(row.id).then(() => {
      ElMessage.success('删除成功')
      getTableData()
    })
  })
}

const handleUpdate = (row) => {
  pageData.editingId = row.id
  pageData.isShowAddDialog = true
}

const tableData = ref([])
const searchFormRef = ref()
const searchData = reactive({
  dictLabel: '',
  dictType:route.query.dictType
})
const getTableData = () => {
  loading.value = true
  queryDictionaryData(searchData, {
    currentPage: paginationData.currentPage,
    size: paginationData.pageSize
  })
    .then((res) => {
      paginationData.total = parseInt(res.data.total)
      tableData.value = res.data.records
    })
    .catch(() => {
      tableData.value = []
    })
    .finally(() => {
      loading.value = false
    })
}
const handleSearch = () => {
  if (paginationData.currentPage === 1) {
    getTableData()
  }
  paginationData.currentPage = 1
}
const resetSearch = () => {
  searchFormRef.value?.resetFields()
  if (paginationData.currentPage === 1) {
    getTableData()
  }
  paginationData.currentPage = 1
}

watch([() => paginationData.currentPage, () => paginationData.pageSize], getTableData, { immediate: true })
</script>

<template>
  <div class="app-container">
    <div v-hasPermi="['sysDictData:post:findPage']" shadow="never" class="search-wrapper">
      <el-form ref="searchFormRef" :inline="true" :model="searchData">
        <el-form-item prop="dictLabel" label="字典标签">
          <el-input v-model="searchData.dictLabel" placeholder="请输入字典标签" maxlength="20" clearable/>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" :icon="Search" @click="handleSearch">查询</el-button>
          <el-button :icon="Refresh" @click="resetSearch">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div v-loading="loading" shadow="never">
      <div class="toolbar-wrapper">
        <div>
          <el-button v-hasPermi="['sysDictData:post:add']" type="primary" plain :icon="Plus" @click="pageData.isShowAddDialog = true,pageData.editingId = ''">新增</el-button>
        </div>
      </div>
      <div class="table-wrapper">
        <el-table :data="tableData" max-height="64vh">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column prop="dictLabel" label="字典标签" align="center" />
          <el-table-column prop="dictValue" label="字典值" align="center" />
          <el-table-column prop="dictSort" label="字典顺序" align="center" />
          <el-table-column prop="status" label="状态" align="center">
            <template #default="scope">
              <el-tag v-if="scope.row.status === 0" type="success" effect="plain">启用</el-tag>
              <el-tag v-else type="danger" effect="plain">停用</el-tag>
            </template>
          </el-table-column>
          <el-table-column fixed="right" label="操作" width="150" align="center">
            <template #default="scope">
              <el-button v-hasPermi="['sysDictData:put:update']" type="primary" text bg size="small" @click="handleUpdate(scope.row)">修改</el-button>
              <el-button v-hasPermi="['sysDictData:delete:delete']" type="danger" text bg size="small" @click="handleDelete(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="flex justify-end">
        <el-pagination
          background
          :layout="paginationData.layout"
          :page-sizes="paginationData.pageSizes"
          :total="paginationData.total"
          :page-size="paginationData.pageSize"
          :currentPage="paginationData.currentPage"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :small="true"
        />
      </div>
    </div>
    <AddDictionaryValue
      ref="addDictionaryValueRef"
      v-model:visible="pageData.isShowAddDialog"
      :id="pageData.editingId"
      @create="createDic"
      @update="updateDic"
    />
  </div>
</template>

<style lang="scss" scoped>
.search-wrapper {
  :deep(.el-card__body) {
    padding-bottom: 2px;
  }
}

.table-wrapper {
  margin-bottom: 20px;
}
</style>
